package com.retrofit.View;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.myapplicationndk.R;


/**
 * @author
 * @date 2020/3/16.
 * GitHub：
 * email：
 * description：
 */
public class CustomCountDownView extends View {

    private Context context;
    private int mRingColor;
    private float mRingWidth;
    private int mRingProgessTextSize = 15;
    private int mProgessTextColor;
    private int mCountdownTime;
    private Paint mPaint;
    private int mCurrentProgress;
    private int mWidth;
    private int mHeight;
    private RectF mRectF;
    private OnCountDownFinishListener mListener;

    public CustomCountDownView(Context context) {
        super(context);
        this.context = context;
    }

    public CustomCountDownView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(attrs);
    }


    public CustomCountDownView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(attrs);

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CustomCountDownView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
        init(attrs);
    }

    private void init(AttributeSet attrs) {

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CountDownView);

        mRingColor = a.getColor(R.styleable.CountDownView_ringColor, context.getResources().getColor(R.color.black));

        mRingWidth = a.getFloat(R.styleable.CountDownView_ringWidth, 15);

        mRingProgessTextSize = a.getDimensionPixelSize(R.styleable.CountDownView_progressTextSize, 50);

        mProgessTextColor = a.getColor(R.styleable.CountDownView_progressTextColor, context.getResources().getColor(R.color.gray));

        mCountdownTime = a.getInteger(R.styleable.CountDownView_countdownTime, 10);

        a.recycle();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        mPaint.setAntiAlias(true);

        this.setWillNotDraw(false);
    }

    public void setCountdownTime(int mCountdownTime) {

        this.mCountdownTime = mCountdownTime;

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        super.onLayout(changed, left, top, right, bottom);

        mWidth = getMeasuredWidth();

        mHeight = getMeasuredHeight();

        mRectF = new RectF(0 + getPaddingLeft() + mRingWidth / 2, 0 + getPaddingTop() + mRingWidth / 2,
                mWidth - getPaddingRight() - mRingWidth / 2, mHeight - getPaddingBottom() - mRingWidth / 2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setColor(mRingColor);

        mPaint.setStyle(Paint.Style.STROKE);

        mPaint.setStrokeWidth(mRingWidth);
        /**
         *  360 - mCurrentProgress  逆时针
         *  mCurrentProgress - 360 顺时针
         */
        canvas.drawArc(mRectF, -90, 360 - mCurrentProgress, false, mPaint);

        Paint textPaint = new Paint();

        textPaint.setAntiAlias(true);

        textPaint.setTextAlign(Paint.Align.CENTER);

        String text = mCountdownTime - (int) (mCurrentProgress / 360f * mCountdownTime) + "";

        textPaint.setTextSize(mRingProgessTextSize);

        textPaint.setColor(mProgessTextColor);

        //文字居中显示
        Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();

        int baseline = (int) ((mRectF.bottom + mRectF.top - fontMetrics.bottom - fontMetrics.top) / 2);

        canvas.drawText(text, mRectF.centerX(), baseline, textPaint);

    }

    private ValueAnimator getValA(long countdownTime) {

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 100);

        valueAnimator.setDuration(countdownTime);

        valueAnimator.setInterpolator(new LinearInterpolator());

        valueAnimator.setRepeatCount(0);

        return valueAnimator;
    }

    /**
     * 开始倒计时
     */
    public void startCountDown() {
        setClickable(false);

        ValueAnimator valueAnimator = getValA(mCountdownTime * 1000);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                float i = Float.valueOf(String.valueOf(animation.getAnimatedValue()));

                mCurrentProgress = (int) (360 * (i / 100f));

                invalidate();
            }
        });

        valueAnimator.start();

        valueAnimator.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {

                super.onAnimationEnd(animation);

                //倒计时结束回调
                if (mListener != null) {

                    mListener.countDownFinished();

                }

                setClickable(true);
            }

        });
    }

    public void setAddCountDownListener(OnCountDownFinishListener mListener) {

        this.mListener = mListener;
    }

    public interface OnCountDownFinishListener {

        void countDownFinished();
    }

}
